package com.ruoyi.project.monitor.job.task;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.rest.detail.mapper.RestOrderDetailMapper;
import com.ruoyi.project.rest.order.mapper.RestOrderMapper;
import com.ruoyi.project.rest.statistics.domain.RestStatisticsData;
import com.ruoyi.project.rest.statistics.mapper.RestStatisticsDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component("StatisticsTask")
public class StatisticsTask implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(StatisticsTask.class);

    @Autowired
    private RestOrderMapper restOrderMapper;
    @Autowired
    private RestStatisticsDataMapper restStatisticsDataMapper;

    /**
     * 统计头一天的销售数据
     */
    public void dayStatistics() {
        logger.info("-------------------开始执行统计任务---------------------");
        String startTime = null;
        //查询最后一次统计实际
        RestStatisticsData statisticsData = restStatisticsDataMapper.selectLastTime();
        if (statisticsData != null) {
            startTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, statisticsData.getStatisticsTime());
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        String endTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, cal.getTime());
        List<RestStatisticsData> list = restOrderMapper.selectStatisticsQuery(startTime, endTime);
        for (RestStatisticsData s : list) {
            restStatisticsDataMapper.insertRestStatisticsData(s);
        }

    }

    @Override
    public void run(String... args) throws Exception {
        dayStatistics();
    }
}
